import { watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { judgePortName } from '@/units'

// 监听路由 判断是否是返回
const useWathRouter = (props) => {
  const { path, backEvent } = props
  const router = useRouter()
  watch(
    () => router.currentRoute.value.path,
    (newValue, oldValue) => {
      if (newValue == path) {
        backEvent && backEvent()
      }
    },
    { immediate: true }
  )
}

const useJumpCompanyDetails = () => {
  const router = useRouter()
  const route = useRoute()

  /**
   *
   * @param {*} enterpriseId
   * @param {*} query
   *
   */
  const goto = async ({ enterpriseId, query = {} }) => {
    const getpath = {
      park: '/zfenterprise-details',
      enterprise: '/qyenterprise-details',
    }

    // if (
    //   enterpriseId &&
    //   loginMode == 'enterprise' &&
    //   route.path === '/qydocking/mate-resources'
    // ) {
    //   router.push({
    //     path: `/qydocking/mate-resources-enterprise/${enterpriseId}`,
    //     query: query,
    //   })
    // } else if (enterpriseId && loginMode === 'park') {
    //   router.push({
    //     path: `${getpath[loginMode]}/${enterpriseId}`,
    //     query: query,
    //   })
    // }

    if (enterpriseId) {
      router.push({
        path: `${getpath[judgePortName()]}/${enterpriseId}`,
        query: query,
      })
    }
  }
  return {
    goto,
  }
}

export { useWathRouter, useJumpCompanyDetails }
